#!/bin/bash

# Flutter App Bundle一键打包脚本
# 专门用于构建App Bundle (推荐用于Google Play上传)

set -e

# 颜色定义
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'

print_info() {
    echo -e "${BLUE}$1${NC}"
}

print_success() {
    echo -e "${GREEN}$1${NC}"
}

# 检查FVM
if [ -f ".fvm/fvm_config.json" ]; then
    FLUTTER_CMD="fvm flutter"
else
    FLUTTER_CMD="flutter"
fi

print_info "==== Flutter App Bundle构建 ===="

# 获取依赖
print_info "获取依赖包..."
$FLUTTER_CMD pub get

# 构建App Bundle
print_info "构建App Bundle..."
$FLUTTER_CMD build appbundle --release

# 显示结果
if [ -f "build/app/outputs/bundle/release/app-release.aab" ]; then
    FILE_SIZE=$(du -h "build/app/outputs/bundle/release/app-release.aab" | cut -f1)
    print_success "✓ App Bundle构建成功!"
    print_info "文件位置: build/app/outputs/bundle/release/app-release.aab"
    print_info "文件大小: $FILE_SIZE"
else
    echo "构建可能失败，请检查输出"
fi